package com.yangfei.mystudy1020.OGL20.Renderer;

import android.content.Context;
import android.opengl.GLSurfaceView;

import com.yangfei.mystudy1020.OGL20.utils.ShaderHelper;
import com.yangfei.mystudy1020.OGL20.utils.TextResourceReader;
import com.yangfei.mystudy1020.R;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES20.GL_FLOAT;
import static android.opengl.GLES20.GL_LINES;
import static android.opengl.GLES20.GL_POINTS;
import static android.opengl.GLES20.GL_TRIANGLES;
import static android.opengl.GLES20.glClear;
import static android.opengl.GLES20.glClearColor;
import static android.opengl.GLES20.glDrawArrays;
import static android.opengl.GLES20.glEnableVertexAttribArray;
import static android.opengl.GLES20.glGetAttribLocation;
import static android.opengl.GLES20.glGetUniformLocation;
import static android.opengl.GLES20.glUniform4f;
import static android.opengl.GLES20.glUseProgram;
import static android.opengl.GLES20.glVertexAttribPointer;
import static android.opengl.GLES20.glViewport;

/**
 * Created by yangfei on 2017/12/12.
 */

public class FirstShaderOGLRenderer implements GLSurfaceView.Renderer{
    private static final int POSITION_COMPONENT_COUNT = 2;
    private static final int BYTES_PER_FLOAT = 4;
    private static final String U_COLOR = "u_Color";
    private static final String  A_POSITION = "a_Position";
    private int uColorLocation;
    private int aPositionLocation;
    private FloatBuffer vertexData;
    private Context mContext;
    private int program;

    public FirstShaderOGLRenderer(Context ctx) {
        mContext = ctx;

        float[] tableVerticles = {
                0f,0f,
                0f,14f,
                9f,14f,
                9f,0f
        };

        float[] tableVerticlesWithTriangles = {
                //triangle 1
                -0.5f,-0.5f,
                0.5f,0.5f,
                -0.5f,0.5f,
                //triangle 2
                -0.5f,-0.5f,
                0.5f,-0.5f,
                0.5f,0.5f,
                //line1
                -0.5f,0f,
                0.5f,0f,
                //mallets
                0f,-0.25f,
                0f,0.25f
        };

        vertexData = ByteBuffer.allocateDirect(tableVerticlesWithTriangles.length*BYTES_PER_FLOAT)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();

        vertexData.put(tableVerticlesWithTriangles);
    }

    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        glClearColor(0,0,0,0);
        String vertexShaderSource = TextResourceReader.readTextFileFromResouce(mContext, R.raw.simple_vertex_shader);
        String fragmentShaderSource = TextResourceReader.readTextFileFromResouce(mContext,R.raw.simple_fragment_shader);

        int vertexShader = ShaderHelper.compileVertexShader(vertexShaderSource);
        int fragmentShader = ShaderHelper.compileFragmentShader(fragmentShaderSource);

        program = ShaderHelper.linkProgram(vertexShader,fragmentShader);
        ShaderHelper.validateProgram(program);
        glUseProgram(program);

        uColorLocation = glGetUniformLocation(program,U_COLOR);
        aPositionLocation = glGetAttribLocation(program,A_POSITION);

        vertexData.position(0);
        glVertexAttribPointer(aPositionLocation,POSITION_COMPONENT_COUNT,GL_FLOAT,false,0,vertexData);

        glEnableVertexAttribArray(aPositionLocation);
    }

    @Override
    public void onSurfaceChanged(GL10 gl10, int i, int i1) {
        glViewport(0,0,i,i1);
    }

    @Override
    public void onDrawFrame(GL10 gl10) {
        glClear(GL_COLOR_BUFFER_BIT);

        glUniform4f(uColorLocation,1.0f,1.0f,1.0f,1.0f);
        glDrawArrays(GL_TRIANGLES,0,6);

        glUniform4f(uColorLocation,1.0f,0.0f,0.0f,1.0f);
        glDrawArrays(GL_LINES,6,2);

        glUniform4f(uColorLocation,0.0f,0.0f,1.0f,1.0f);
        glDrawArrays(GL_POINTS,8,1);

        glUniform4f(uColorLocation,1.0f,0.0f,0.0f,1.0f);
        glDrawArrays(GL_POINTS,9,1);
    }
}
